//
//  CMDBUserManageTableView.swift
//  CMDB
//
//  Created by 王磊 on 23/05/2017.
//  Copyright © 2017 CMDB. All rights reserved.
//

import UIKit
extension CMDBUserManageTableView {
    
    override class func getInstance() -> CMDBUserManageTableView {
        
        return CMDBUserManageTableView(frame: CGRectZero, style: .Plain)
    }
}


class CMDBUserManageTableView: UITableView {
    
    override init(frame: CGRect, style: UITableViewStyle) {
        super.init(frame: frame, style: style)
        
        configOwnSubviews()
    }
    
    private var users: Array<CMDBUserInfoBean> = []
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
extension CMDBUserManageTableView {
    
    override func configOwnSubviews() {
        super.configOwnSubviews()
        
        dataSource = self
        
        delegate = self
        
        showsVerticalScrollIndicator = false
        
        showsHorizontalScrollIndicator = false
        
        bounces = false
        
        separatorStyle = .None
        
        keyboardDismissMode = .OnDrag
        
        backgroundColor = UIColor.clearColor()
    }
}

extension CMDBUserManageTableView {
    
    internal func updateData(data: Array<CMDBUserInfoBean>?) {
        
        if let data = data {
            
            users += data
            
            reloadData()
        }
    }
    internal func updateData(data: CMDBUserInfoBean, for indexPath: NSIndexPath) {
        
        users.replaceRange(0..<1, with: [data])
        
        reloadSections(NSIndexSet(index: 0), withRowAnimation: .Fade)
    }
}

extension CMDBUserManageTableView: UITableViewDataSource {}

extension CMDBUserManageTableView {
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        
        return users.count
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return 1
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        var cell = tableView.dequeueReusableCellWithIdentifier("CellId") as? CMDBUserManageTableViewCell
        
        if cell == nil {
            
            cell = CMDBUserManageTableViewCell(style: .Default, reuseIdentifier: "CellId")
        }
        
        cell?.update(users[indexPath.section])
        
        return cell!
    }
}


extension CMDBUserManageTableView: UITableViewDelegate {}

extension CMDBUserManageTableView {
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        
        let argu = TSArguInfo()
        
        argu.view = self
        
        argu.data = users[indexPath.section]
        
        argu.itemType = .TableViewItem
        
        performSelWithArgu(argu)
    }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        return 64
    }
    
}
